package hot100;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

public class Solution49 {
    public List<List<String>> groupAnagrams(String[] strs) {
        HashMap<String, List<String>> map = new HashMap<>();
        for (String str : strs) {
            int[] hash = new int[26];
            for (int i = 0; i < str.length(); i++) {
                hash[str.charAt(i) - 'a']++;
            }
            StringBuffer sb = new StringBuffer();
            for (int i = 0; i < 26; i++) {
                if (hash[i] != 0) {
                    sb.append((char) (i + 'a')).append(hash[i]);
                }
            }
            List<String> tmp = map.getOrDefault(sb.toString(), new ArrayList<>());
            tmp.add(str);
            map.put(sb.toString(), tmp);
        }
        List<List<String>> res = new ArrayList<>(map.values());
        return res;
    }
}
